package cn.csc.module.shop.framework.job.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration(proxyBeanMethods = false)
public class ShopJobConfiguration {

    public static final String THREAD_POOL_TASK_EXECUTOR = "THREAD_POOL_TASK_EXECUTOR";

    @Value("${csc.threadPool.corePoolSize:8}")
    private Integer corePoolSize;
    @Value("${csc.threadPool.maxPoolSize:80}")
    private Integer maxPoolSize;
    @Value("${csc.threadPool.keepAliveSeconds:60}")
    private Integer keepAliveSeconds;
    @Value("${csc.threadPool.queueCapacity:200}")
    private Integer queueCapacity;

    @Bean(THREAD_POOL_TASK_EXECUTOR)
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize); // 设置核心线程数
        executor.setMaxPoolSize(maxPoolSize); // 设置最大线程数
        executor.setKeepAliveSeconds(keepAliveSeconds); // 设置空闲时间
        executor.setQueueCapacity(queueCapacity); // 设置队列大小
        executor.setThreadNamePrefix("shop-task-"); // 配置线程池的前缀
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 进行加载
        executor.initialize();
        return executor;
    }

}
